﻿using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WLWZGao6.Domain.Common;
using WLWZGao6.Domain.IRepository;
using WLWZGao6.Domain.Model;
using WLWZGao6.EndCode;
using WLWZGao6.Domain.Dto;

namespace WLWZGao6.EndCode.Repository
{
    public class UserRepository : IUserRepository
    {
        public UserDataAccess _userDataAccess;
        public UserRepository(UserDataAccess userDataAccess)
        {
            _userDataAccess = userDataAccess;
        }
        //用户登录
        public EData Login(UserLoginDto user)
        {
            var ss = _userDataAccess.User.Where(x => x.UserName.Contains(user.UserName)).ToList();
            if(ss.Count()!=0)
            {
                var dd=_userDataAccess.User.Where(x=>x.Userpass.Contains(user.UserPass)).ToList();
                if(dd.Count()!=0)
                {
                    return new EData() { Code = Endata.Success, Msg = "登录成功!" };
                }
                else
                {
                    return new EData() { Code = Endata.Error, Msg = "账号不存在,请重新输入账号或密码" };
                }
            }
            else
            {
                return new EData() { Code = Endata.Error, Msg = "账号不存在,请重新输入账号或密码" };
            }
        }
        //用户添加
        public EData UserAdd(User user)
        {
            _userDataAccess.User.Add(user);
            _userDataAccess.SaveChanges();
            return new EData() { Code = Endata.Success, Msg = "添加成功!" };
        }
        //用户删除
        public EData UserDel(int Ids)
        {
            var ss = _userDataAccess.User.Where(x => x.Id == Ids).SingleOrDefault();
            _userDataAccess.User.RemoveRange(ss);
            return new EData() { Code = Endata.Success, Msg = "删除成功!" };
        }
        //用户显示
        public EData UserShow(string Name)
        {
            var ss = _userDataAccess.User.ToList();
            if (!string.IsNullOrEmpty(Name))
            {
                ss = ss.Where(x => x.Name.Contains(Name)).ToList();
            }
            return new EData() { Code = Endata.Success, Msg = "显示成功!",Data=ss};
        }
        //用户修改
        public EData UserUpd(int Ids, User user)
        {
            var ss = _userDataAccess.User.Where(x => x.Id == Ids).SingleOrDefault();
            ss = user;
            _userDataAccess.SaveChanges();
            return new EData() { Code = Endata.Success, Msg = "修改成功!" };
        }
        //用户添加角色
        public EData UserRoleAdd(int Ids, string roleId)
        {
            var ss = _userDataAccess.userRole.Where(x => x.Id == Ids).ToList();
            var cc = ss.Count();
            int affectRows = _userDataAccess.Database.ExecuteSqlInterpolated(
                $"delete from userRole where RoleId ={Ids}");

            var dd = roleId.Split(',');
            foreach (var item in dd)
            {
                affectRows += _userDataAccess.Database.ExecuteSqlInterpolated(
                    $"insert into userRole values({Ids},{item})");
            }
            _userDataAccess.SaveChanges();
            return new EData() { Code = Endata.Success, Msg = "添加成功!" };
        }
    }
}
